package cn.b.sky.mes.pp.service.impl;

import java.util.List;

import cn.b.sky.mes.pp.dao.OrderTypeDao;
import cn.b.sky.mes.pp.model.OrderType;
import cn.b.sky.mes.pp.model.condition.OrderTypeCondition;
import cn.b.sky.mes.pp.service.OrderTypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * 
 * <strong>Title : OrderTypeServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.service.impl<br></strong>
 * <strong>Description : </strong>订单类型服务实现类<br> 
 * <strong>Create on : 2016年1月7日 上午9:47:53<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service
public class OrderTypeServiceImpl implements OrderTypeService {
  
  private static final Logger LOG = LoggerFactory.getLogger(OrderTypeServiceImpl.class);

  @Autowired
  OrderTypeDao orderTypeDao;

  private static final String DEFAULT_ORDER_FIELD = "CreateDateTime";

  @Override
  public List<OrderType> getOrderTypesByCondition(OrderTypeCondition condition) {
    if (null == condition) {
      condition = new OrderTypeCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField(DEFAULT_ORDER_FIELD);
    }
    return orderTypeDao.selectOrderTypesByCondition(condition);
  }

  @Override
  public void saveOrderType(OrderType orderType) {
    if (StringUtils.isBlank(orderType.getSiteCode()) || StringUtils.isBlank(orderType.getOrderTypeCode())) {
      return;
    }
    orderTypeDao.insertOrderType(orderType);
  }

  @Override
  public void modifyOrderType(OrderType orderType) {
    if (StringUtils.isBlank(orderType.getSiteCode()) || StringUtils.isBlank(orderType.getOrderTypeCode())) {
      return;
    }
    orderTypeDao.updateOrderType(orderType);
  }

  @Override
  public BatchJobReport deleteOrderTypeByPk(String siteCode, String orderTypeCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(orderTypeCode)) {
      return new BatchJobReport("", "siteCode or defectTypeCode is null", false, languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = orderTypeCode;
    boolean success = true;
    String cause = "";

    try {
      orderTypeDao.deleteOrderTypeByPK(siteCode, orderTypeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public OrderType getOrderType(String siteCode, String orderTypeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(orderTypeCode)) {
      return null;
    }
    return orderTypeDao.getOrderType(siteCode, orderTypeCode);
  }

  @Override
  public int getOrderTypesCountByCondition(OrderTypeCondition condition) {
    if (null == condition) {
      condition = new OrderTypeCondition();
    }
    return orderTypeDao.selectOrderTypeCountByCondition(condition);
  }

  @Override
  public List<OrderType> getAllUnlockedOrderTypes(String siteCode) {

    return orderTypeDao.selectAllUnlockedOrderTypes(siteCode);
  }

  @Override
  public List<OrderType> getAllOrderTypes(String siteCode) {
    return orderTypeDao.selectAllOrderTypes(siteCode);
  }

}
